{
    "collab_server" : "",
    "contents" : "---\ntitle: \"Cluster Analysis\"\nauthor: \"Jessica Blois\"\ndate: \"8/2/2018\"\noutput: html_document\n---\n\n## Cluster analysis examples\n\n```{r package_loading, message=F, warning=F}\n#uncomment the following line if you haven't installed these packages yet\n#install.packages(c('vegan', 'cluster', 'rioja', 'gplots', 'adjclust)) \n\nlibrary('vegan')\nlibrary('cluster')\nlibrary('rioja')\nlibrary('gplots')\nlibrary('adjclust')\n\n# also uses stats package, included with base R.\n```\n\n## Load data\n\nWe can use the Samwell Cave and pollen datasets from yesterday:\n\n```{r load_data}\nload(file=\"data/scpdFauna_cleaned.RData\") #object is scpdSM \npollen12K <- read.csv(file=\"data/12000.interp.data_clean_sites.csv\")\n```\n\n## Play around with clustering analyses\n\n### Step 1: Create dissimilary matrix\n\nWe will apply three different distance measures to our data: euclidean, bray-curtis, and chao.\n```{r dissim}\nscpdSM_euc <- vegdist(scpdSM, method=\"euclidean\")\nscpdSM_bray <- vegdist(scpdSM, method=\"bray\")\nscpdSM_chao <- vegdist(scpdSM, method=\"chao\")\n\nprint(scpdSM_euc)\nprint(scpdSM_bray)\nprint(scpdSM_chao)\n```\n\n### Step 2: cluster samples according to different algorithms\n```{r perform_cluster_analysis}\nclust_euc_ward.D2 <- hclust(scpdSM_euc, method=\"ward.D2\")\nclust_euc_neighbor <- hclust(scpdSM_euc, method=\"single\")\nclust_euc_upgma <- hclust(scpdSM_euc, method=\"average\") #UPGMA\nclust_euc_wpgma <- hclust(scpdSM_euc, method=\"mcquitty\") #WPGMA\n\nclust_bray_ward.D2 <- hclust(scpdSM_bray, method=\"ward.D2\")\nclust_bray_neighbor <- hclust(scpdSM_bray, method=\"single\")\nclust_bray_upgma <- hclust(scpdSM_bray, method=\"average\") #UPGMA\nclust_bray_wpgma <- hclust(scpdSM_bray, method=\"mcquitty\") #WPGMA\n\nclust_chao_ward.D2 <- hclust(scpdSM_chao, method=\"ward.D2\")\nclust_chao_neighbor <- hclust(scpdSM_chao, method=\"single\")\nclust_chao_upgma <- hclust(scpdSM_chao, method=\"average\") #UPGMA\nclust_chao_wpgma <- hclust(scpdSM_chao, method=\"mcquitty\") #WPGMA\n```\n\n## Compare cluster patterns\n\n### Plot simple dendograms\n```{r plot_dendro}\nplotting_objs <- ls()[grep(\"clust\", ls())]\n\n# Note: I'm writing the cluster diagram to a pdf file, so it's easier to read. You can uncomment the pdf() and dev.off() lines to print to the screen, or just not copy those lines\npdf(file=\"output/cluster_diagrams.pdf\", height=11, width=8.5)\n  par(mfcol=c(4,3), mar=c(2,4,1,1)+.01)\n  for (i in 1:length(plotting_objs)){\n    plot(get(plotting_objs[i]), cex=0.75, xlab=NA, sub=NA, main=plotting_objs[i], hang=-.1)\n  }\ndev.off()\n```\n\n### Plot heat maps of the results\n\n```{r heatmap}\ndissimColors <- gray.colors(30, start = 0.9, end = 0.1, gamma = 2.2, alpha = NULL)\nages <- as.numeric(rownames(scpdSM))\nageColors <- rainbow(length(ages), start=0, end=4/6)\nheatmap(scpdSM, col=dissimColors, distfun=function(m) vegdist(m,method=\"bray\"), hclustfun=function(m) hclust(m, method=\"average\"), RowSideColors = ageColors)\n```\n\nNote 1: I've added some colors to the age labels. If adjacent strata clustered together, the colors would be ordered in a gradient.\nNote 2: in this case the dendrogram for the taxon names is meaningless, so let's suppress it. But we can't do this with the base heatmap function. We need to use a function in a different package: gplots\n\n```{r heatmap.2}\nheatmap.2(scpdSM, dendrogram='row', col=dissimColors, distfun=function(m) vegdist(m,method=\"bray\"), hclustfun=function(m) hclust(m, method=\"average\"), RowSideColors = ageColors, tracecol=NA)\n```\n\n## Constrained clustering\n\nSee the reference paper in the refs folder (Grimm 1987) for more info on the CONISS implementation.\n\n```{r}\nconstrained <- chclust(scpdSM_bray, method=\"coniss\")\nbstick <- bstick(constrained, 10)\nngroups <- min(which(bstick$dispersion>bstick$bstick))\nx <- strat.plot(scpdSM, yvar=ages, title=\"\", y.rev=T, cex.xlabel=0.5,cex.yaxis=0.5, cex.axis=0.5, clust=constrained) \naddClustZone(x, constrained, col=\"red\", ngroups)\n```\n\n\n## Exercises\n\n1. Generate some example data, calculate dissimilarity, and then hand-calculate the dendrogram using one clustering algorithm\n2. Explore cluster analysis using the fossil pollen data.\n  - Produce a pollen diagram with a constrained cluster diagram using the fossil pollen data for 12,000 years ago. \n  - How many groups/zones should be displayed? \n\n\n## Alternate packages for constrained clustering\n\nI ran across this package for constrained clustering: adjclust.  See this website <https://github.com/rstats-gsoc/gsoc2017/wiki/Constrained-Hierarchical-Agglomerative-Clustering> for an in-depth discussion\n\n\n\n```{r}\nfit1 <- adjClust(scpdSM_bray, type=\"dissimilarity\")\nplot(fit1, leaflab=\"perpendicular\")\n```\n\n\n",
    "created" : 1533217490894.000,
    "dirty" : false,
    "encoding" : "UTF-8",
    "folds" : "",
    "hash" : "3143033228",
    "id" : "1CBAE01D",
    "lastKnownWriteTime" : 1533230026,
    "last_content_update" : 1533230914690,
    "path" : "~/Dropbox/Service/2018-Analytical Paleobiology Shortcourse/Multivariate-Day 2-blois/ClusterAnalysis.Rmd",
    "project_path" : "ClusterAnalysis.Rmd",
    "properties" : {
        "last_setup_crc32" : "",
        "tempName" : "Untitled1"
    },
    "relative_order" : 1,
    "source_on_save" : false,
    "source_window" : "",
    "type" : "r_markdown"
}